package org.tof.hotel.servlet;

import org.apache.log4j.Logger;
import org.tof.hotel.dao.AdministratorDaoImpl;
import org.tof.hotel.dto.AdministratorDTO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

public class GetAdmin {
    static Logger logger = Logger.getLogger(GetAdmin.class);

    public void run(HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        AdministratorDTO dto = new AdministratorDTO(
                request.getParameter("firstName"),
                request.getParameter("lastName"),
                request.getParameter("phone"),
                request.getParameter("email"),
                request.getParameter("address"));

        final PrintWriter w = response.getWriter();
        w.write("<html><body>");
        w.write("<H1>Search results</H1>");

        AdministratorDaoImpl dao;
        ResultSet rs;
        try {
            dao = new AdministratorDaoImpl();
            rs = dao.select(dto);

            ArrayList<StringBuilder> strings = new ArrayList<StringBuilder>();
            StringBuilder s = new StringBuilder();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            // plain output
//            for (int i = 1; i <= columnCount; i++) {
//                s.append(metaData.getColumnName(i) + " | ");
//            }
//            strings.add(s.append("<br/>"));
//            s = new StringBuilder();
            //
//            while (rs.next()) {
//                for (int i = 1; i <= columnCount; i++) {
//                    s.append(rs.getString(i) + " | ");
//                }
//                strings.add(s.append("<br/>"));
//                s = new StringBuilder();
//            }
//            for (StringBuilder sb : strings) {
//                w.write(sb.toString());
//            }
            // normal output
            int ctr = 0;
            while (rs.next()) {
                ctr++;
                strings.add(new StringBuilder("Row #" + ctr + ": </br>"));
                for (int i = 1; i <= columnCount; i++) {
                    s.append(metaData.getColumnName(i) + ":" + rs.getString(i) + " </br>");
                }
                strings.add(s);
                s = new StringBuilder();
            }
            for (StringBuilder sb : strings) {
                w.write(sb.toString());
            }
            if (strings.isEmpty()) {
                w.write("<H2>There are no Administrators with the specified parameters</H2>");
            }
        } catch (SQLException e) {
            logger.warn(e.toString(), e);
            w.write(e.toString());
        } catch (ClassNotFoundException e) {
            logger.error(e.toString(), e);
        } catch (IOException e) {
            logger.error(e.toString(), e);
        }


        w.write("</body></html>");
    }
}
